/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.cs.stats.ZimbraPerf;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import java.util.TreeMap;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetServerStats
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  40 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*  42 */     Server localServer = Provisioning.getInstance().getLocalServer();
/*  43 */     checkRight(zsc, context, localServer, Rights.Admin.R_getServerStats);
/*     */     
/*     */ 
/*  46 */     List<Element> eStats = request.listElements("stat");
/*  47 */     Set<String> requestedNames = new HashSet();
/*  48 */     for (int i = 0; i < eStats.size(); i++) {
/*  49 */       requestedNames.add(((Element)eStats.get(i)).getAttribute("name"));
/*     */     }
/*     */     
/*     */ 
/*  53 */     Map<String, Object> allStats = ZimbraPerf.getStats();
/*  54 */     Map<String, Object> returnedStats = new TreeMap();
/*  55 */     boolean returnAllStats = requestedNames.size() == 0;
/*     */     
/*  57 */     for (String name : allStats.keySet()) {
/*  58 */       if ((returnAllStats) || (requestedNames.contains(name))) {
/*  59 */         returnedStats.put(name, allStats.get(name));
/*  60 */         requestedNames.remove(name);
/*     */       }
/*     */     }
/*     */     
/*  64 */     if (requestedNames.size() != 0) {
/*  65 */       StringBuilder buf = new StringBuilder("Invalid stat name");
/*  66 */       if (requestedNames.size() > 1) {
/*  67 */         buf.append("s");
/*     */       }
/*  69 */       buf.append(": ").append(StringUtil.join(", ", requestedNames));
/*  70 */       throw ServiceException.FAILURE(buf.toString(), null);
/*     */     }
/*     */     
/*     */ 
/*  74 */     Element response = zsc.createElement(AdminConstants.GET_SERVER_STATS_RESPONSE);
/*  75 */     for (String name : returnedStats.keySet()) {
/*  76 */       String stringVal = toString(returnedStats.get(name));
/*  77 */       Element eStat = response.addElement("stat").addAttribute("name", name).setText(stringVal);
/*     */       
/*     */ 
/*     */ 
/*  81 */       String description = ZimbraPerf.getDescription(name);
/*  82 */       if (description != null) {
/*  83 */         eStat.addAttribute("description", description);
/*     */       }
/*     */     }
/*     */     
/*  87 */     return response;
/*     */   }
/*     */   
/*     */   private static String toString(Object value) {
/*  91 */     if (value == null) {
/*  92 */       return null;
/*     */     }
/*  94 */     if (((value instanceof Double)) || ((value instanceof Float))) {
/*  95 */       return String.format("%.2f", new Object[] { value });
/*     */     }
/*  97 */     return value.toString();
/*     */   }
/*     */   
/*     */ 
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 103 */     relatedRights.add(Rights.Admin.R_getServerStats);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/GetServerStats.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */